<?php
/**
 * 《网络地址》实现类
 *
 * @author 孙晓晔
 * @version $Id: Zone.class.php 804 2009-12-30 10:05:41Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Ip extends InfoUtil {

	public $tbl = Constant::tbl_ip;

	/**
	 * 状态
	 */
	public $status = array(
		'-' => '请选择',
		'1' => '已使用',
		'2' => '未使用',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'name' => '地址',
		'createtime' => '创建时间',
		'modifytime' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$province = $this->code_option('4');
		$controller->assign_by_ref('province_option', $province);

		$controller->display($request, 'info/ip/find');
		return true;
	}

	function _where($request) {
		$ip_s = $request['ip_s'];
		$where = " WHERE 1=1";

		if (!empty($ip_s)) {
			$where .= " AND ip like '" . $ip_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/ip/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/zone/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$province = $request['province'];
		$zone = $request['zone'];
		$city = $request['city'];

		$orderby = $request['orderby'];

		$status = $request['status'];

		clean($this->tbl . "_status_" . $status);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(province, zone, city, orderby";
		$fmt .= ", status, createtime, modifytime) VALUES('%s', '%s', '%s'";
		$fmt .= ", '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $province, $zone, $city, $orderby, $status);
		// debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/zone/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$province = $request['province'];
		$zone = $request['zone'];
		$city = $request['city'];

		$orderby = $request['orderby'];

		$status = $request['status'];

		clean($this->tbl . "_status_" . $status);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET province = '%s', zone = '%s', city = '%s', orderby = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $province, $zone, $city, $orderby, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function importPost($controller, $request) {
		$this->pretreat($controller);

		$data = $request['data'];
		// echo $data;

		$aa = preg_split("/[\\n]+/i", $data);
		// print_r($a);

		$province = '';
		$db = newdb($this->dsn);
		while (list($k, $v) = each($aa)) {
			$buf = trim($v);
			if (strlen($buf) < 5) {
				continue;
			}

			$a = preg_split("/[\t|\s]+/i", $buf);
			// print_r($a);
			if (count($a) < 2) {
				$province = $buf;
				continue;
			}

			$city = trim($a[0]);
			$zone = trim($a[1]);

			$fmt = "INSERT INTO %s(`province`, city, zone) VALUES('%s', '%s', '%s')";
			$sql = sprintf($fmt, $this->tbl, $province, $city, $zone);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/ip/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['mac_x'] = $this->_mac($row['ip']);
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function _ip($ip) {
		$db = newdb($this->tbl);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while($row = $db->fetch_array($result)) {
			if ($row['status'] == '2' || $row['ip'] == $ip) {
				$ret[$row['ip']] = $row['ip'];
			}
		}

		return $ret;
	}
	
	function _update1($ip) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '1', modifytime = now() WHERE ip = '%s'";
		$sql = sprintf($fmt, $this->tbl, $ip);
		// debug($sql);
		$db->query($sql);
	}
	
	function _update2($ip1, $ip2) {
		$db = newdb($this->dsn_w);

		if ($ip1 <> $ip2) {
			$fmt = "UPDATE %s SET status = '1', modifytime = now() WHERE ip = '%s'";
			$sql = sprintf($fmt, $this->tbl, $ip2);
			// debug($sql);
			$db->query($sql);

			$fmt = "UPDATE %s SET status = '2', modifytime = now() WHERE ip = '%s'";
			$sql = sprintf($fmt, $this->tbl, $ip1);
			// debug($sql);
			$db->query($sql);
		}
	}

	/**
	 * 更新
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function update($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		$fmt = "update %s a, %s b set a.emp = b.emp, a.status = 1 where a.ip = b.ip";
		$sql = sprintf($fmt, Constant::tbl_ip, Constant::tbl_emp_mac);
		$db->query($sql);

		$fmt = "UPDATE %s SET status = '2'";
		$sql = sprintf($fmt, Constant::tbl_ip);
		$db->query($sql);

		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, Constant::tbl_ip);
		// echo $sql . "\n";
		$result = $db->query($sql);
		$fmt = "UPDATE %s SET status = '1' WHERE id = '%s'";
		while ($row = $db->fetch_array($result)) {
			if ($this->_mac($row['ip'])) {
				$sql = sprintf($fmt, Constant::tbl_ip, $row['id']);
				$db->query($sql);
				echo "U";
			}
		}

		logdate($controller, $request);
		return true;
	}

	function _mac($ip) {
		require_once(dirname(__FILE__) . '/../emp/Mac.class.php');
		$aa = new Mac();
		
		return $aa->_mac_ip($ip);
	}
}
?>